import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * The space is te escenary where the user play, this escenary is used like main of the game.
 * 
 * @author (Arturo Aaron Rios Obregon) 
 * @version (25 june 2013)
 */
public class Juego extends World
{
    private Hero heroe;
    private Counter level;
    private Counter score;
    private Counter life;
    private Counter bomb;
    private Counter munition;
    private int numEne;
    private int finalLevel;
    private int contEne;
    private ArrayList l;
    private boolean eneFin;
    private boolean pierde;
    private GreenfootSound s;
    private GreenfootImage [] images;
    private int numImage;
    
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Juego()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1,false); 
        setPaintOrder(Counter.class,LaserMadre.class,IniLaser.class,BulletEnemy.class,Laser.class,Hero.class,Enemy.class,MotherShip.class);
        heroe = new Hero();
        addObject(heroe,300,300);
        level = new Counter("Level: ");
        addObject(level,50,15);
        level.setValue(1);
        life = new Counter("Life: ");
        addObject(life,290,15);
        life.setValue(heroe.getVida());
        score = new Counter("Score: ");
        addObject(score,170,15);
        munition = new Counter("Munition: ");
        addObject(munition,410,15);
        munition.setValue(heroe.getAmmo());
        bomb = new Counter("Bomb: ");
        addObject(bomb,530,15);
        bomb.setValue(heroe.getBomb());
        eneFin=false;
        finalLevel=8;
        numEne=10;
        contEne=0;
        pierde = false;
        s = new GreenfootSound("gamesound.mp3");
        images = new GreenfootImage[4];
        images[0] = new GreenfootImage("fondo1.jpg");
        images[1] = new GreenfootImage("fondo2.jpeg");
        images[2] = new GreenfootImage("fondo3.jpg");
        images[3] = new GreenfootImage("fondo4.jpg");
        numImage=0;
        
    }
    
    /**
     * This method is used like the main.
     */
    public void act()
    {
        
        l=(ArrayList)getObjects(Enemy.class);
        if(!s.isPlaying())
            s.play();
        if(eneFin==true&&l.isEmpty()&&pierde==false)
        {
            capturaPuntaje();
            animacionGanaste();
            
            pierde=true;
        }
        if(l.isEmpty()&&level.getValue()!=finalLevel&&pierde==false)  //crea la horda
        {
            for(int i=0;i<5;i++)
                creaEnemigoR();
            contEne+=5;
        }
        if(level.getValue()==finalLevel&&eneFin==false&&l.isEmpty()&&pierde==false) //nivel final
        {
            MotherShip m = new MotherShip();
            addObject(m,300,10);
            eneFin=true;
            
        }
        if(contEne==numEne&&l.isEmpty()&&level.getValue()!=finalLevel&&pierde==false) //aumenta nivel
        {
            numEne+=5;
            contEne=0;
            level.add(1);
            if(level.getValue()%2==0)
                imagenes();
        }        
        
        actualiza();
        if(heroe.getVida()==0&&pierde==false)
        {
            capturaPuntaje();
            animacionPerdiste();            
            pierde=true;
        }
    }
    
    /**
     * This method save the records of the user.
     */
    public void capturaPuntaje()
    {
        if (UserInfo.isStorageAvailable()) 
        {
            UserInfo myInfo = UserInfo.getMyInfo();
            if (score.getValue() > myInfo.getScore()) 
            {
                myInfo.setScore(score.getValue());
                myInfo.store();  
            }
        }

    }
    
    /**
     * This method change the images for the background
     */
    public void imagenes()
    {                
        GreenfootImage img = images[numImage];
        setBackground(img);
        numImage+=1;
    }
    
    /**
     * Method which create an annimation when the player loose
     */
    public void animacionGanaste()
    {
        int x=heroe.getX(), y=heroe.getY();
        l=(ArrayList)getObjects(Enemy.class);
        removeObjects(l);
        l=(ArrayList)getObjects(Counter.class);
        removeObjects(l);
        l=(ArrayList)getObjects(Bala.class);
        removeObjects(l);
        l=(ArrayList)getObjects(Item.class);
        removeObjects(l);
        /*while(y!=0)
        {
        if(x>getWidth()/2)
            heroe.setLocation(x-1,y);
        else if(x<getWidth()/2)
            heroe.setLocation(x+1,y);
        else if(y>0)
            heroe.setLocation(x,y-1);
        }*/
        removeObject(heroe);   
        setBackground("win.jpg");
        s.stop();
        Greenfoot.delay(300);
        Greenfoot.setWorld(new Menu());
    }
    
    /**
     * Method which create an annimation when the player loose
     */
    public void animacionPerdiste()
    {
        l=(ArrayList)getObjects(Enemy.class);
        removeObjects(l);
        l=(ArrayList)getObjects(Counter.class);
        removeObjects(l);
        l=(ArrayList)getObjects(Bala.class);
        removeObjects(l);
        l=(ArrayList)getObjects(Item.class);
        removeObjects(l);
        for(int i=0;i<7;i++)
        {
            heroe.explota();
            Greenfoot.delay(15);
        }
        removeObject(heroe);
        setBackground("spaceartt.jpeg");
        Greenfoot.delay(300);
        s.stop();
        Greenfoot.setWorld(new Menu());
    }
    
    /**
     * This methos actualize the counters on the game.
     */
    public void actualiza()
    {
        life.setValue(heroe.getVida());
        munition.setValue(heroe.getAmmo());
        bomb.setValue(heroe.getBomb());
    }
    
    /**
     * This method help to up the score of the player.
     */
    public void aumentaPuntos(int cant)
    {
        score.add(cant);
    }
    
    /**
     * This method create the enemys on the game.
     */
    public void creaEnemigoR()
    {
        switch(Greenfoot.getRandomNumber(5)%level.getValue())
        {
            case 0:
                    Meteor n1 = new Meteor();
                    addObject(n1,randomX(),30);
                    break;
            case 1:
                    BasicEnemy n2 = new BasicEnemy();
                    addObject(n2,randomX(),30);
                    break;
            case 2:
                    NormalEnemy n3 = new NormalEnemy();
                    addObject(n3,randomX(),30);
                    break;  
            case 3:
                    DupEnemy n4 = new DupEnemy();
                    addObject(n4,randomX(),30);
                    break;  
            case 4:
                    ProducerEnemy n5 = new ProducerEnemy();
                    addObject(n5,randomX(),30);
                    break;
                    
        }
    }
    
    /**
     * This method give a random position on x.
     */
    public int randomX()
    {
        int num=0;
        switch(Greenfoot.getRandomNumber(6))
        {
            case 0:
                num= 50;
                break;
            case 1:
                num= 150;
                break;
            case 2:
                num= 250;
                break;
            case 3:
                num= 350;
                break;
            case 4:
                num= 450;
                break;
            case 5:
                num= 550;
                break;
        }
        return num;
    }
    
    /**
     * This method return the hero.
     */
    public Hero getHero()
    {
        return heroe;
    }
    
    /**
    * When the execution of the program stop the music stop too.
    */
    public void stopped()
    {
        s.pause();
    }
    
    /**
     * When the execution of the program is reanuded the music reanuded too.
     */
    public void started()
    {
        s.play();
    }
}
